package com.meihua.shellapp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.meihua.shellapp.R;

/**
 * Created by ${zhangyuanchao} on 2017/10/19.
 */

public class TestPassDialog extends Dialog {
    private Context mContext;
    private String mCount;
    private String mcon_txt2;
    private String mcon_txt1;
    boolean isPass;

    public TestPassDialog(@NonNull Context context, @StyleRes int themeResId,String count,String txt1,String txt2, boolean b) {
        super(context, themeResId);
        mContext = context;
        mCount = count;
        mcon_txt1 = txt1;
        mcon_txt2 = txt2;
        isPass = b;
    }

    protected TestPassDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
    }
    public interface OnClickListener {
        void okClick(View view);
    }

    private OnClickListener clickListener;

    public void setOnTestClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_test_pass, null);
        setContentView(inflate);
        setCancelable(false);
        TextView tv_count = inflate.findViewById(R.id.tv_count);
        TextView tv_con_txt1 = inflate.findViewById(R.id.tv_con_txt1);
        TextView tv_con_txt2 = inflate.findViewById(R.id.tv_con_txt2);
        TextView tv_submit = inflate.findViewById(R.id.tv_submit);

        tv_count.setText(mCount);
        tv_count.setTextColor(Color.parseColor(isPass?"#ff9c06":"#9b9b9b"));
        tv_con_txt1.setText(mcon_txt1);
        tv_con_txt2.setText(mcon_txt2);
        tv_submit.setText(isPass?"完成":"重新测试");
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickListener.okClick(view);
            }
        });
        //设置dialog大小
        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) mContext).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialogWindow.setAttributes(params);
    }
}
